<?php
define('FWAD_XMLTABLE_ACTION_VIEW',0);
define('FWAD_XMLTABLE_ACTION_DEFAULT',0);
define('FWAD_XMLTABLE_ACTION_CONVERT',1);

class ModuleXmlTableAdmin extends ModuleAdmin
{
	function __construct(&$AdminController)
	{
		parent::__construct($AdminController);
	}
	public function GetPage($Query)
	{
		define('FWAD_XMLTABLE_TPL', $this->controller->cfg['fw_path']."modules/xmltable/tpls/");
		$this->_requestHandler('FWAD_XMLTABLE_ACTION_',FWAD_XMLTABLE_ACTION_DEFAULT,$_GET);
		switch ($this->_action)
		{
			case FWAD_XMLTABLE_ACTION_VIEW:
				$this->controller->main_template = FWAD_MAIN_TPL;
				$this->controller->content_template = FWAD_XMLTABLE_TPL."index_tpl.html";
				break;
			case FWAD_XMLTABLE_ACTION_CONVERT:
				$this->controller->main_template = FWAD_MAIN_TPL;
				$this->controller->content_template = FWAD_XMLTABLE_TPL."index_tpl.html";
				require_once('../lib/class.XmlTable2HTMLTable.php');
				
				$HTMLTableParams = array();
		$HTMLTableParams['useTH'] = isset($_POST['useTH'])? true : false;		
		if (isset($_POST['classEven']))		$HTMLTableParams['classEven']	= $_POST['classEven'];
		if (isset($_POST['classOdd']))		$HTMLTableParams['classOdd']	= $_POST['classOdd'];
		$HTMLTableParams['TR'] = array();
		if (isset($_POST['onmovr']))		$HTMLTableParams['TR']['onmouseover']		= $_POST['onmovr'];
		if (isset($_POST['onmout']))		$HTMLTableParams['TR']['onmouseout']		= $_POST['onmout'];
		$HTMLTableParams['TABLE'] = array();
		$HTMLTableParams['TABLE']['border']		= 0;
		$HTMLTableParams['TABLE']['cellspacing']	= 1;
		$HTMLTableParams['TABLE']['cellpadding']	= 0;
		$HTMLTableParams['TABLE']['id']			= "itemmodels";
		$HTMLTableParams['TABLE']['class']		= "itemmodels";
		$test = XmlTable2HTMLTable::ConvertFile('E:\\myhtml\\elit-design\\downloads\\prices\\20.xml');
				/*
				$result = $this->XmlTable2HTMLTable($_POST['xmltext']);
				
				//$result = $this->XmlTable2HTMLTable($_POST['xmltext']);
				//$this->controller->tpl->assign("Result",$result);				
				*/
				break;
		}
	}
	public function XmlTable2HTMLTable($xmltext)
	{
		$result = "";
		$xmltext = utf8_encode($xmltext);
		$xml = new XMLReader();
		$xml->XML($xmltext,"cp1251");
		$wait_text = false;
		while($xml->read())
		{
			if ($xml->nodeType == XMLReader::ELEMENT)
			{
				$name = strtolower($xml->name);
				if ($name==='table')
					$result.="<table>";
				elseif ($name==='row')
					$result.="<tr>";
				elseif ($name==='cell')
				{
					$result.="<td ";
					if ($xml->hasAttributes) 
					{
						$rowspan = $xml->getAttribute('ss:MergeDown');
						$colspan = $xml->getAttribute('ss:MergeAcross');
						if ($rowspan!="") {$rowspan++; $result.="rowspan={$rowspan} ";}
						if ($colspan!="") {$colspan++; $result.="colspan={$colspan} ";}
					}
					$result.=">";
				}
				elseif ($name==='data')
					$wait_text = true;
					//$result.="<td>";
				//elseif ($name===XMLReader::TEXT)				
			}
			if (($xml->nodeType===XMLReader::TEXT)&& $wait_text)
			{
				$result.=utf8_decode($xml->value);
				$wait_text = false;
			}
			if ($xml->nodeType == XMLReader::END_ELEMENT)
			{
				$name = strtolower($xml->name);
				if ($name==='table')
					$result.="</table>";
				elseif ($name==='row')
					$result.="</tr>";
				elseif ($name==='cell')
					$result.="</td>";
			}	
		}
		return $result;
	}
}
?>